package com.inforeach.util.string;

import java.util.Arrays;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class ChainedStringTransformer_utest
{
    IStringTransformer sut_;

    @Before
    public void setUp()
    {
        sut_ = new ChainedStringTransformer(Arrays.asList(new RevStringTransformer(), new AppStringTransformer()));
    }

    void check(String str, String fwd)
    {
        Assert.assertEquals("forward", fwd, sut_.forward(str));
        Assert.assertEquals("reverse", str, sut_.reverse(fwd));
    }

    @Test
    public void test()
    {
        check("", "{}");
        check("abc", "{cba}");
    }

    static class RevStringTransformer implements IStringTransformer
    {
        public String forward(String str)
        {
            return inv(str);
        }

        public String reverse(String str)
        {
            return inv(str);
        }

        String inv(String str)
        {
            return new StringBuilder(str).reverse().toString();
        }
    }

    static class AppStringTransformer implements IStringTransformer
    {
        public String forward(String str)
        {
            return "{" + str + "}";
        }

        public String reverse(String str)
        {
            return str.substring(1, str.length() - 1);
        }
    }
}
